#include "CsvReader.h"

CsvReader* CsvReader::m_csvReader = NULL;

CsvReader* CsvReader::getInstance(){
	if (NULL == m_csvReader){
		m_csvReader = new CsvReader();
		if (m_csvReader && m_csvReader->init()){
			m_csvReader->autorelease();
			m_csvReader->retain();
		}
		else{
			CC_SAFE_DELETE(m_csvReader);
			m_csvReader = NULL;
		}
	}
	return m_csvReader;
}

bool CsvReader::init(){
	return true;
}

ValueVector CsvReader::split(const char* srcStr, const char* sSep){
	ValueVector stringList;

	int size = strlen(srcStr);

	Value str = Value(srcStr);

	int startIndex = 0;
	int endIndex = 0;
	endIndex = str.asString().find(sSep);

	std::string lineStr;

	while (endIndex > 0)
	{
		lineStr = str.asString().substr(startIndex, endIndex);
		stringList.push_back(Value(lineStr));
		str = Value(str.asString().substr(endIndex + 1, size));

		endIndex = str.asString().find(sSep);
	}

	if (str.asString().compare("") != 0){
		stringList.push_back(Value(str.asString()));
	}

	return stringList;
}